package com.yupi.project.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置类：配置Exchange、Queue、以及绑定交换机
 * @author Fzs
 * @Description: 发送验证码的消息队列
 */
@Configuration
@EnableRabbit
public class RabbitMQConfig {

    @Value("${rabbitmq.queue}")
    private String queueName;

    @Value("${rabbitmq.exchange}")
    private String exchangeName;

    @Value("${rabbitmq.routingKey}")
    private String routingKey;

    @Value("${rabbitmq.deadQueue}")
    private String deadQueue;

    @Value("${rabbitmq.deadQueue.routingKey}")
    private String deadQueueRoutingKey;

    @Value("${rabbitmq.deadQueue.exchange}")
    private String deadQueueExchange;

    @Value("${rabbitmq.bind.dead.queue}")
    private String commonQueue;

    @Value("${rabbitmq.bind.dead.exchange}")
    private String commonExchange;

    @Value("${rabbitmq.bind.dead.routingKey}")
    private String commonRoutingKey;

    // 死信队列
    @Bean
    public Queue deadQueue() {
        System.out.println("死信队列 = >" + deadQueue);
        return QueueBuilder.durable(deadQueue).build();
    }

    // 死信交换机
    @Bean
    DirectExchange deadExchange() {
        System.out.println("死信队列交换机 => " + deadQueueExchange);
        return new DirectExchange(deadQueueExchange);
    }

    // 死信交换机绑定死信队列
    @Bean
    Binding bindDead() {
        System.out.println("死信队列匹配键 = >" + deadQueueRoutingKey);
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(deadQueueRoutingKey);
    }

    // 声明与死信队列绑定的普通队列
    @Bean
    public Queue commonQueue() {
        System.out.println("普通队列为 => " + commonQueue);
        return QueueBuilder.durable(commonQueue)
                .deadLetterExchange(deadQueueExchange)
                .deadLetterRoutingKey(deadQueueRoutingKey)
                // 给队列设置过期时长
                .ttl(60000)
                .build();
    }

    // 声明普通交换机
    @Bean
    DirectExchange commonExchange() {
        System.out.println("普通交换机 => " + commonExchange);
        return new DirectExchange(commonExchange);
    }

    // 绑定队列和交换机
    @Bean
    Binding commonBind() {
        System.out.println("普通队列和交换机绑定的匹配键 = > " + commonRoutingKey);
        return BindingBuilder.bind(commonQueue()).to(commonExchange()).with(commonRoutingKey);
    }


    //队列 起名：CodeDirectQueue
    @Bean
    public Queue CodeDirectQueue() {
        System.out.println("队列为 = >" + queueName);
        return new Queue(queueName,true);
    }

    //Direct交换机 起名：CodeDirectExchange
    @Bean
    DirectExchange CodeDirectExchange() {
        System.out.println("交换机为 = >" + exchangeName);
        return new DirectExchange(exchangeName);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：CodeDirectRouting
    @Bean
    Binding bindingDirect() {
        System.out.println("匹配键为 => " + routingKey);
        return BindingBuilder.bind(CodeDirectQueue()).to(CodeDirectExchange()).with(routingKey);
    }

}
